Conversation
KyNorthstar
left a comment
There was a problem hiding this comment.
I love what you've done to simplify this! I just have a few questions and requests
| var Enum = // the name of the "class" | ||
| function() // this is what the arguments are passed to | ||
| { | ||
| var values = arguments; // get the varargs and save them to a 'values' variable. | ||
| var self = { // prepare a 'self' object to return, so we work with an object instead of a function | ||
| all : [], // prepare a list of all indices | ||
| keys : values // create the list of all keys | ||
| }; | ||
|
|
||
| for(var i = 0; i < values.length; i++) // for all enum names given | ||
| { | ||
| self[values[i]] = i; // add the variable to this object | ||
| self.all[i] = i; // add the index to the list of all indices | ||
| } | ||
|
|
||
| return self; // return the 'self' object, instead of this function | ||
| const Enum = (...elements) => { | ||
| const s = { | ||
| all: [], | ||
| keys: elements | ||
| } | ||
| ; No newline at end of file | ||
|
|
||
| elements.map((el, i) => { | ||
| s[el] = s.all[i] = i | ||
| }) | ||
|
|
||
| return s | ||
| } |
There was a problem hiding this comment.
Thanks for modernizing this! The changes make a lot of sense!
However, this "DOCUMENTED" file is overly-compacted. It was originally unrolled and commented to help explain how the code works, so removing the comments, shortening variable names (s, el), and combining assignments (s[el] = s.all[i] = i) makes it more difficult to understand what's going on
| @@ -1 +1 @@ | |||
| Enum=function(){let v=arguments,s={all:[],keys:v};for(let i=v.length;i--;)s[v[i]]=s.all[i]=i;return s;} | |||
| module.exports=Enum=((...l)=>(s={all:[],keys:l},l.map((l,a)=>s[l]=s.all[a]=a),s)); No newline at end of file | |||
There was a problem hiding this comment.
This is awesome!
Can you help me understand why you reassign module.exports here as well as in index.js?
| { | ||
| "name": "simple-enum", | ||
| "version": "1.0.0", | ||
| "description": "A very, very tiny JavaScript enum implementation", | ||
| "main": "index.js", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/tolgaek/node-enum" | ||
| }, | ||
| "author": "Tolga Ekmen <tolga.ekmen@gmail.com>", | ||
| "license": "MIT" | ||
| } |
There was a problem hiding this comment.
Can you help me understand why this metadata was removed? It seems useful to maintain attribution and description
| @@ -1 +1 @@ | |||
| module.exports = require('./lib/micro-enum.js'); | |||
| module.exports=require('./lib/micro-enum.js'); No newline at end of file | |||
There was a problem hiding this comment.
This is only used on the developer side, right? So the user doesn't download this, if I understand correctly. What do these whitespace removals improve?
| A **94-byte** JavaScript enum implementation. | ||
| A **67-byte** JavaScript enum implementation. | ||
|
|
||
| Hazer-hazer version of `tolgaek` 94-byte JavaScript enum implementation. |
There was a problem hiding this comment.
This downplays the original creators. It'd be kinder to say that @tolgaek and I originally wrote this, and you improved upon it.
No description provided.